package com.jtny.nytb.equipment.vo.dataparam;

import cn.hutool.core.util.ObjectUtil;
import com.jtny.component.util.BeanUtils;
import com.jtny.nytb.config.datafactor.dto.ConfigAlarmDTO;
import com.jtny.nytb.config.datafactor.dto.ConfigDataParamManagementDTO;
import com.jtny.nytb.config.datafactor.dto.ConfigLabelDTO;
import com.jtny.nytb.config.datafactor.dto.ConfigParamDTO;
import com.jtny.nytb.equipment.dto.dataparam.AlarmTempDTO;
import com.jtny.nytb.equipment.dto.dataparam.DataParamTempDTO;
import com.jtny.nytb.equipment.dto.dataparam.LabelTempDTO;
import com.jtny.nytb.valid.annotation.ValueValid;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.List;

/**
 * 数据参量tempVO
 *
 * @author 智慧能源中心 - 伍天宇
 * @package com.jtny.nytb.equipment.vo
 * @fileName DataParamTempVO.java
 * @createTime 2023年09月15日 15:46
 * @Copyright© 2023 jtny
 * 江投能源技术研究院 版权所有
 */
@Data
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(value = "DataParamTempVO", description = "数据参量tempVO")
public class DataParamTempVO extends DataParamBaseVO implements Serializable {

    /**
     * 关联类型 1表示 relationId 为规则id， 2表示 relationId 为模型id
     */
    @ApiModelProperty(value = "关联类型 1表示 relationId 为规则id， 2表示 relationId 为模型id", required = true)
    @ValueValid(intValues = {1, 2}, message = "关联类型只能为1或2", ignoreEmpty = false)
    private Integer relationType;

    /**
     * 规则id或模型id
     */
    @ApiModelProperty(value = "规则id或模型id", required = true)
    @NotNull(message = "规则id或模型id不能为空")
    private Long relationId;

    /**
     * 设备id
     */
    @ApiModelProperty(value = "设备id", hidden = true)
    private Long equipmentId;

    /**
     * 设备code
     */
    @ApiModelProperty(value = "设备code", hidden = true)
    private String equipmentCode;

    @ApiModelProperty(value = "设备名称", hidden = true)
    private String equipmentName;

    /**
     * 是否配电监测展示
     */
    @ApiModelProperty(value = "规则id或模型id", required = true)
    @NotNull(message = "规则id或模型id不能为空")
    private Boolean powerCheckDisplay;

    /**
     * 记点频率
     */
    @ApiModelProperty(value = "记点频率")
    private FrequencyVO frequency;

    /**
     * 记点算法信息
     */
    @ApiModelProperty("记点算法信息")
    private AlgorithmVO algorithm;

    /**
     * 标签列表
     */
    @ApiModelProperty("标签列表")
    private List<LabelVO> labelList;

    /**
     * 报警列表
     */
    @ApiModelProperty("报警列表")
    private List<AlarmVO> alarmList;

    @ApiModelProperty("是否来自规则复制")
    private Boolean isCopyFromRule;

    public DataParamTempDTO buildDTO2() {
        DataParamTempDTO dataParamTempDTO = super.buildDTO2();
        dataParamTempDTO.setRelationType(relationType);
        dataParamTempDTO.setRelationId(relationId);
        dataParamTempDTO.setPowerCheckDisplay(powerCheckDisplay);
        if (ObjectUtil.isNotNull(frequency)) {
            dataParamTempDTO.setFrequency(frequency.getFrequency());
            dataParamTempDTO.setFrequencyUnit(frequency.getFrequencyUnit());
        }
        if (ObjectUtil.isNotNull(algorithm)) {
            dataParamTempDTO.setAlgorithm(algorithm.buildTempDTO());
        }
        dataParamTempDTO.setLabelList(BeanUtils.copyToList(labelList, LabelTempDTO.class));
        dataParamTempDTO.setAlarmList(BeanUtils.copyToList(alarmList, AlarmTempDTO.class));
        return dataParamTempDTO;
    }

    public DataParamTempDTO buildDTO() {
        DataParamTempDTO dataParamTempDTO = super.buildDTO();
        dataParamTempDTO.setRelationType(relationType);
        dataParamTempDTO.setRelationId(relationId);
        dataParamTempDTO.setPowerCheckDisplay(powerCheckDisplay);
        if (ObjectUtil.isNotNull(frequency)) {
            dataParamTempDTO.setFrequency(frequency.getFrequency());
            dataParamTempDTO.setFrequencyUnit(frequency.getFrequencyUnit());
        }
        if (ObjectUtil.isNotNull(algorithm)) {
            dataParamTempDTO.setAlgorithm(algorithm.buildTempDTO());
        }
        dataParamTempDTO.setLabelList(BeanUtils.copyToList(labelList, LabelTempDTO.class));
        dataParamTempDTO.setAlarmList(BeanUtils.copyToList(alarmList, AlarmTempDTO.class));
        return dataParamTempDTO;
    }

    public ConfigDataParamManagementDTO buildConfigDataParamManagementDTO() {
        ConfigDataParamManagementDTO dataParamTempDTO = super.buildConfigDataParamManagementDTO();
        if (ObjectUtil.isNotNull(frequency)) {
            dataParamTempDTO.setFrequency(frequency.getFrequency());
            dataParamTempDTO.setFrequencyUnit(frequency.getFrequencyUnit());
        }
        if (ObjectUtil.isNotNull(algorithm)) {
            dataParamTempDTO.setAlgorithmName(algorithm.getAlgorithmName());
            dataParamTempDTO.setAlgorithmId(algorithm.getAlgorithmId());
            dataParamTempDTO.setConfigAlgorithDTO(algorithm.buildDTO());
            dataParamTempDTO.setConfigParamDTOList(BeanUtils.copyToList(algorithm.getConfigParamList(), ConfigParamDTO.class));
        }

        dataParamTempDTO.setConfigAlarmDTOList(BeanUtils.copyToList(alarmList, ConfigAlarmDTO.class));
        return dataParamTempDTO;
    }


    public DataParamTempVO(DataParamTempDTO dataParamTempDTO) {
        super(dataParamTempDTO);
        this.relationType = dataParamTempDTO.getRelationType();
        this.relationId = dataParamTempDTO.getRelationId();
        if (ObjectUtil.isNotNull(dataParamTempDTO.getFrequency())) {
            this.frequency = new FrequencyVO(dataParamTempDTO.getFrequency(), dataParamTempDTO.getFrequencyUnit());
        }
        this.powerCheckDisplay = dataParamTempDTO.getPowerCheckDisplay();
        this.algorithm = new AlgorithmVO(dataParamTempDTO.getAlgorithm());
        this.labelList = BeanUtils.copyToList(dataParamTempDTO.getLabelList(), LabelVO.class);
        this.alarmList = BeanUtils.copyToList(dataParamTempDTO.getAlarmList(), AlarmVO.class);
    }

}
